<template>
  <el-dialog :visible.sync="visible" width="70%" custom-class="default-dialog"
    :close-on-click-modal="false" @close="cancel" append-to-body>
    <div slot="title">
      <img v-if="type" :src="require(`@/assets/img/${type}.png`)" alt="">
      <span>{{ title }} </span>
    </div>
    <el-form :model="form" :rules="rules" ref="form" label-width="90px" class="dialog-form" :disabled="type == 'view'">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="名称" prop="menuName">
            <el-input size="small" v-model="form.menuName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="form.menuType != 3">
          <el-form-item label="图标" prop="icon">
            <icon-picker v-model="form.icon"  />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" v-if="form.menuType != 3">
        <el-col :span="12">
          <el-form-item label="组件名称" prop="componentName">
            <el-input size="small" v-model="form.componentName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="组件路径" prop="componentPath">
            <el-input size="small" v-model="form.componentPath"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="菜单类型" prop="menuType">
            <el-radio-group v-model="form.menuType" size="small">
              <el-radio-button  v-for="item in menuTypeList" :key="item.dictDetailGuid" :label="+item.value">{{ item.label }}</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用" prop="status">
            <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="上级菜单" prop="menuParentId">
            <el-cascader
              ref="organRef"
              size="small"
              v-model="menu"
              :options="menuList"
              :props="props"
              :show-all-levels="false"
              filterable
              @change="handleChange"></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序号" prop="sort">
            <el-input-number size="small" v-model="form.sort" :min="1" :max="999"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服务地址" prop="url">
            <el-input size="small" v-model="form.url"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" v-if="type != 'view'" type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      title: '',
      form: {},
      rules: {
        menuName: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        icon: [{ required: true, message: '请选择图标', trigger: 'change' }],
        componentName: [{ required: true, message: '请输入组件名称', trigger: 'blur' }],
        componentPath: [{ required: true, message: '请输入组件地址', trigger: 'blur' }],
        menuType: [{ required: true, message: '请选择菜单类型', trigger: 'change' }],
      },
      type: '',
      menuList: [],
      menuTypeList: [],
      props: { expandTrigger: 'hover',label: 'name',value: 'id',children: 'children' ,checkStrictly: true },
      menu: [],
      expandRowKeys: []
    };
  },
  methods: {
    showModal(type, row) {
      this.visible = true
      this.type = type
      this.getMenuList()
      this.getDictInit()
      if (type == 'add') {
        this.title = '新增'
        this.form = {
          "componentName": "",
          "componentPath": "",
          "description": '',
          "icon": '',
          menuCodePath: '',
          menuName: '',
          menuNamePath: '',
          menuParentId: '0',
          menuParentName: '',
          menuType: 1,
          publish: null,
          sort: null,
          status: 1,
          url: ''
        }
      } else if (type == 'edit') {
        this.title = '编辑'
        this.getInfoInit(row.sysMenuId)
      } else if (type == 'view') {
        this.title = '详情'
        this.getInfoInit(row.sysMenuId)
      }
    },
    getDictInit(){
      // 菜单类型
      this.$api.getDictDetail({dicGuid: '1390053539965696'}).then(res => {
        if(res && res.code == 200){
          let data = res.data || []
          this.menuTypeList = data
        }
      })
    },
    // 获取全部菜单
    getMenuList(){
      this.$api.getMenuTree().then(res => {
        if(res && res.code == 200){
          let data = res.data || []
          this.menuList = data
        }
      })
    },
    // 详情
    getInfoInit(id){
      this.$api.getMenuManageInfo({id:id}).then(res => {
        if(res && res.code == 200){
          let data = res.data || {}
          let menu = data.menuCodePath ? data.menuCodePath.split('/') : ''
          if(menu){
            this.menu = menu.filter(l => l)
          }
          this.form = data
        }
      })
    },
    // 上级菜单
    handleChange(val){
      this.$refs.organRef.dropDownVisible = false;
      if(val){
        let item = this.$refs.organRef.getCheckedNodes()[0]
        this.form.menuParentId = item.path[item.path.length - 1]
        this.form.menuParentName = item.pathLabels[item.pathLabels.length - 1]
        this.form.menuCodePath = '/' +item.path.join('/') + '/'
        this.form.menuNamePath = '/' +item.pathLabels.join('/') + '/'
        this.expandRowKeys = item.path
      }else {
        this.form.menuParentId = ''
        this.form.menuParentName = ''
        this.form.menuCodePath = ''
        this.form.menuNamePath = ''
      }
    },
    // 确定
    sure() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if(this.type == 'add'){
            this.$api.addMenuManage(this.form).then(res => {
              if(res && res.code == 200){
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit',this.expandRowKeys)
              }
            })
          }else if(this.type == 'edit'){
            this.$api.updateMenuManage(this.form).then(res => {
              if(res && res.code == 200){
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit',this.expandRowKeys)
              }
            })
          }
        }
      });
    },
    // 取消
    cancel() {
      this.visible = false;
      this.menu = []
      this.$refs["form"].resetFields();
    },
  }
};
</script>
<style lang="less">
  .ui-fas{
    margin-top: 5px;
    .el-input__inner{
      height: 32px;
      line-height: 32px;
    }
  }
</style>
